package com.poizon.manager.impl;

import com.poizon.bo.CodeBo;
import com.poizon.config.SystemConfig;
import com.poizon.entity.SmsLog;
import com.poizon.enumerate.SmsType;
import com.poizon.manager.intf.SmsService;
import com.poizon.service.intf.SmsLogService;
import com.poizon.utils.CheckUtils;
import com.poizon.utils.JedisUtils;
import com.poizon.utils.NumUtils;
import com.poizon.utils.SmsUtils;
import com.poizon.vo.R;
import org.springframework.stereotype.Service;

/**
 * @ClassName SmsServiceImpl
 * @Author NoCheng
 * @Date 2022/1/2 14:49
 * @Description:TODO 验证码业务逻辑实现层
 */
@Service
public class SmsServiceImpl implements SmsService {
    private final SmsLogService service;

    public SmsServiceImpl(SmsLogService service) {
        this.service = service;
    }

    @Override
    public R sendLoginCode(String phone) {
        //格式校验
        if (CheckUtils.isPhone(phone)) {
            //生成验证码
            int code = NumUtils.createNum(SystemConfig.CODE_LEN);
            //发送验证码
            if (SmsUtils.sendCode(SystemConfig.SMS_LOGIN_CODE_MOULD, phone, code)) {
                //存储验证码 1.有效期 10分钟 Redis
                JedisUtils.addString(SystemConfig.SMS_LOGIN_CODE + phone, code + "", SystemConfig.SMS_CODE_TIME);
                //记录数据到数据库
                service.insert(new SmsLog(SmsType.LOGIN.getValue(), phone, "发送登录验证码"));
                //返回结果
                return R.ok();
            }else {
                return R.fail("发送失败");
            }
        }else {
            return R.fail("手机号格式错误");
        }
        //return R.fail("网络异常，请检查网络");
    }

    @Override
    public R sendFindCode(String phone) {
        //格式校验
        if (CheckUtils.isPhone(phone)) {
            //生成验证码
            int code = NumUtils.createNum(SystemConfig.CODE_LEN);
            //发送验证码
            if (SmsUtils.sendCode(SystemConfig.SMS_FIND_CODE_MOULD, phone, code)) {
                //存储验证码 1.有效期 10分钟 Redis
                JedisUtils.addString(SystemConfig.SMS_FIND_CODE + phone, code + "", SystemConfig.SMS_CODE_TIME);
                //记录数据到数据库
                service.insert(new SmsLog(SmsType.FIND.getValue(), phone, "发送找回密码的验证码"));
                //返回结果
                return R.ok();
            }
        }
        return R.fail("网络异常，请检查网络");
    }

    @Override
    public boolean checkLoginCode(CodeBo bo) {
        //生成 key
        String key = SystemConfig.SMS_LOGIN_CODE + bo.getPhone();
        return checkCode(key, bo.getCode());
    }

    @Override
    public boolean checkFindCode(CodeBo bo) {
        //生成 key
        String key = SystemConfig.SMS_FIND_CODE + bo.getPhone();
        return checkCode(key, bo.getCode());
    }

    @Override
    public boolean checkCode(String key, int code) {
        //校验是否存在验证码
        if (JedisUtils.exists(key)) {
            //比较验证码
            if ((code + "").equals(JedisUtils.getString(key))) {
                //删除验证码，保证一次性
                JedisUtils.delKey(key);
                return true;
            }
        }
        return false;
    }
}
